package hut.ai.team.screen;

import hut.ai.team.actor.Computer;
import hut.ai.team.actor.Player;
import hut.ai.team.inf.ClickInput;
import hut.ai.team.stages.Stages;
import hut.ai.team.table.BanCo;
import hut.ai.team.table.VanCo;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class MainScreen extends AbstractScreen {

	public BanCo mBanCo;
	public int mode;
	public Player n1;
	public Player n2;
	public Computer computer;
	Preferences prefs;
	public Dialog dialog;

	private MyStage stage;
	private TextureRegionDrawable mOption, mOptionPressed, mExit, mExitPressed,
			mUndo, mUndoPressed;

	private MainScreen() {
		instance = this;
		stage = new MyStage();

		TextureAtlas bgAtlas = game.manager.get("images/bg/backgrounds.atlas");
		TextureAtlas uiAtlas = game.manager.get("images/ui/ui.atlas");

		//dialog
		dialog = new Dialog(uiAtlas);
		dialog.setVisible(false);
		
		// Board
		Image bg = new Image(bgAtlas.findRegion("background-game"));
		bg.setFillParent(true);
		stage.addActor(bg);

		// Drawable
		mOption = new TextureRegionDrawable(uiAtlas.findRegion("options1"));
		mOptionPressed = new TextureRegionDrawable(uiAtlas.findRegion("options_mouseover"));
		mExit = new TextureRegionDrawable(uiAtlas.findRegion("exit"));
		mExitPressed = new TextureRegionDrawable(uiAtlas.findRegion("exit_mouseover"));
		mUndo = new TextureRegionDrawable(uiAtlas.findRegion("undo"));
		mUndoPressed = new TextureRegionDrawable(uiAtlas.findRegion("undo_mouseover"));

		// preferences
		prefs = Gdx.app.getPreferences("preferences");

		Button btnOption1 = new Button(mOption, mOptionPressed);
		btnOption1.setPosition(game.convertWidth(111), game.convertHeight(640));
		btnOption1.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				game.setScreen(SettingScreen.getInstance());
			}
		});

		Button btnExit1 = new Button(mExit, mExitPressed);
		btnExit1.setPosition(game.convertWidth(36), game.convertHeight(640));
		btnExit1.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				game.setScreen(MenuScreen.getInstance());
			}
		});

		Button btnUndo1 = new Button(mUndo, mUndoPressed);
		btnUndo1.setPosition(game.convertWidth(184), game.convertHeight(640));
		btnUndo1.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
//				dialog.show(thongbao);
//				dialog.setVisible(true);
			}
		});
		
		Button btnOption2 = new Button(mOption, mOptionPressed);
		btnOption2.setPosition(game.convertWidth(325), game.convertHeight(55));
		btnOption2.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				game.setScreen(SettingScreen.getInstance());
			}
		});

		Button btnExit2 = new Button(mExit, mExitPressed);
		btnExit2.setPosition(game.convertWidth(400), game.convertHeight(55));
		btnExit2.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				game.setScreen(MenuScreen.getInstance());
			}
		});

		Button btnUndo2 = new Button(mUndo, mUndoPressed);
		btnUndo2.setPosition(game.convertWidth(250), game.convertHeight(55));
		btnUndo2.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
//				dialog.show(thongbao);
//				dialog.setVisible(true);
			}
		});
		
		VanCo.KhoiTaoVanCo();
		mBanCo = new BanCo();
		mode = MenuScreen.GameMode;
		System.out.println("mode: " + mode);
		if (mode == 1) {
			// P vs C
			n1 = new Player(1);
			for (int i = 0; i < n1.dsQuanCo.size(); i++) {
				stage.addActor(n1.dsQuanCo.get(i));
			}

			computer = new Computer(2);
			for (int i = 0; i < computer.dsQuanCo.size(); i++) {
				stage.addActor(computer.dsQuanCo.get(i));
			}
			
		} else {
			// P vs P
			n1 = new Player(1);
			for (int i = 0; i < n1.dsQuanCo.size(); i++) {
				stage.addActor(n1.dsQuanCo.get(i));
			}

			n2 = new Player(2);
			for (int i = 0; i < n2.dsQuanCo.size(); i++) {
				stage.addActor(n2.dsQuanCo.get(i));
			}
		}
//		mBanCo.showData();
		stage.addActor(mBanCo);
		stage.addActor(btnUndo1);
		stage.addActor(btnExit1);
		stage.addActor(btnOption1);
		stage.addActor(btnUndo2);
		stage.addActor(btnExit2);
		stage.addActor(btnOption2);
		stage.addActor(dialog);
	}
	

	@Override
	protected void onBackKeyPress() {
		game.setScreen(MenuScreen.getInstance());
	}

	@Override
	public void show() {
		super.show();
		InputMultiplexer inputs = new InputMultiplexer();
		inputs.addProcessor(new ClickInput(stage));
		inputs.addProcessor(stage);
		
		Gdx.input.setInputProcessor(inputs);
//		Gdx.input.setInputProcessor(inputProcessor);
		Gdx.input.setCatchBackKey(true);

		if (prefs.getBoolean("soundOn")) {
			game.sound.nhacnen.play();
			game.sound.nhacnen.setLooping(true);
		} else {
			game.sound.nhacnen.stop();
		}
		
		MenuScreen.BACK = 2;
	}

	@Override
	public Stage getStage() {
		return stage;
	}

	private static MainScreen instance = null;

	public static MainScreen getInstance() {
		if (instance == null) {
			instance = new MainScreen();
		}
		return instance;
	}

	class MyStage extends Stages {

		TextureRegion mBoard;
		TextureRegion mBG;
		SpriteBatch mBatch;

		public MyStage() {
			super(game.assumeWidth, game.assumeHeight, false);

		}
	}

	public class Dialog extends Group {
		public Label lb1;
		LabelStyle lbstyle;

		public Dialog(TextureAtlas uiAtlas) {

			BitmapFont fntSetting = new BitmapFont(
					Gdx.files.internal("font/fontSetting.fnt"),
					uiAtlas.findRegion("fontSetting"), false);

			lbstyle = new LabelStyle(fntSetting, Color.BLUE);

			setSize(stage.getWidth(), stage.getHeight());
			Image shadow = new Image(uiAtlas.findRegion("shadow"));
			shadow.setFillParent(true);
			addActor(shadow);

			Image bgDialog = new Image(uiAtlas.findRegion("dialog"));
			bgDialog.setPosition((stage.getWidth() - bgDialog.getWidth()) / 2,
					(stage.getHeight() - bgDialog.getHeight()) / 2);
			addActor(bgDialog);

			Image ImgCup = new Image(uiAtlas.findRegion("cup"));
			ImgCup.setPosition((stage.getWidth() - ImgCup.getWidth()) / 2,
					((stage.getHeight() - ImgCup.getHeight()) / 2)+50);
			addActor(ImgCup);
		}

		public void show(String string) {
			lb1 = new Label("" + string, lbstyle);
			lb1.setPosition(game.convertWidth(138), game.convertHeight(291));
			lb1.setAlignment(Align.center);
			lb1.setWrap(true);
			lb1.setWidth(273);
			addActor(lb1);
			
			addListener(new ClickListener() {
				@Override
				public void clicked(InputEvent event, float x, float y) {
					setVisible(false);
				}
			});
		}
	}
}
